//跑的快金币模式
module.exports.createRoom = function(roomId,channelService,GameRemote) {
	var basic = require("./hunanRunQuickly.js").createRoom
	var room = new basic(roomId,channelService,GameRemote)
	room.roomMode = "hunanRunQuicklyGold"
	room.maxScore = 0   			//最大分数
	room.minScore = 0 				//最小分数
	room.rate = 0 					//倍率
	room.charge = 0 				//服务费
	room.willWin = false 			//只赢不输
	room.graded = false 			//梯度结算
	room.beginMode = 2
	room.inventory = 10000 			//库存
	var local = {}
	console.log("hunanRunQuicklyGold!!!!")
	//设置游戏信息
	room.setBeforeInfo = function(info) {
		if(info.maxScore && typeof(info.maxScore) == "number" && info.maxScore >= 0){
			room.maxScore = info.maxScore
		}
		if(info.minScore && typeof(info.minScore) == "number" && info.minScore >= 0){
			room.minScore = info.minScore
		}
		if(info.rate && typeof(info.rate) == "number" && info.rate >= 0){
			room.rate = info.rate
		}
		if(info.charge && typeof(info.charge) == "number" && info.charge >= 0){
			room.charge = info.charge
		}
		if(info.willWin === true){
			room.willWin = true
		}
		if(info.graded === true){
			room.graded = true
		}
		if(info.roomMode){
			room.roomMode = info.roomMode
		}
		if(info.matchType){
			room.matchType = info.matchType
		}
		room.acquireInventory()
	}
	//更新库存
	room.updateInventory = function(value) {
		if(room.rate > 10){
			GameRemote.dbService.db.hincrby("game:gold:inventory",room.matchType,value,function(err,data) {
				room.acquireInventory()
			})
		}
	}
	//获取最新库存
	room.acquireInventory = function() {
		GameRemote.dbService.db.hget("game:gold:inventory",room.matchType,function(err,data) {
			console.log("game:gold:inventory : ",data)
			room.inventory = parseInt(data) || 0
			room.checkInventory()
		})
	}
	//检查库存   库存小于0时降低玩家好运值
	room.checkInventory = function() {
		if(room.rate > 10){
			if(room.inventory <= 0){
				console.log("库存低 : "+room.inventory)
				var rate = 1
				for(var i = 0;i < room.players.length;i++){
					if(room.players[i] && room.players[i].uid < 10000){
						room.luckys[i] = 80 * rate
					}else{
						room.luckys[i] = 0
					}
				}
				console.log("库存低 : ",room.inventory,room.luckys)
			}else{
				for(var i = 0;i < room.players.length;i++){
					if(room.players[i] && room.players[i].uid < 10000){
						room.luckys[i] = 0
					}
				}
			}
		}
	}
	room.joinLaterFun = function(chair) {
		room.luckys[chair] = 0
		if(room.players[chair].playerInfo.contorl){
			room.luckys[chair] += parseInt(room.players[chair].playerInfo.contorl)
		}
		room.checkInventory()
	}
	room.getLocal().changeScore = function(chair,score) {
		if(!room.getLocal().councilScores[chair]){
			room.getLocal().councilScores[chair] = 0
		}
		score *= room.rate
		if((room.getLocal().councilScores[chair] + score) <= 0 && room.willWin === true){
			//只赢不输场不计负分
			room.getLocal().councilScores[chair] = 0
		}else{
			room.getLocal().councilScores[chair] += score
		}
		var notify = {
			cmd : "changeScore",
			chair : chair,
			changeScore : score,
			council : room.getLocal().councilScores[chair]
		}
		room.sendAll(notify)
	}
	//重写结算
	room.getLocal().settlement = function() {
		clearTimeout(room.playTimer)
		var allScore = 0
		var allGold = 0
		if(room.graded){
			//梯度结算
			for(var i = 0;i < 3;i++){
				if(i !== room.getLocal().curPlayer && room.players[i].isReady){
					var tmpScore = room.getLocal().handRest[i]
					if(tmpScore === 16){
						tmpScore *= 4
					}else if(tmpScore > 10 && tmpScore <= 15){
						tmpScore *= 3
					}else if(tmpScore > 5 && tmpScore <= 10){
						tmpScore *= 2
					}
					allScore += tmpScore
					room.getLocal().changeScore(i,-tmpScore)
				}
			}
		}else{
			for(var i = 0;i < 3;i++){
				if(i !== room.getLocal().curPlayer && room.players[i].isReady){
					var tmpScore = room.getLocal().handRest[i]
					//春天加倍
					if(room.getLocal().handRest[i] === 16){
						tmpScore *= 2
					}
					allScore += tmpScore
					room.getLocal().changeScore(i,-tmpScore)
				}
			}
		}
		room.getLocal().changeScore(room.getLocal().curPlayer,allScore)
		var tmpArr = []
		//金币统计
		room.forEachReady(function(player) {
			if(player.uid > 10000){
				var tmpObj = {
					uid : player.uid,
					score : room.getLocal().councilScores[player.chair]
				}
				tmpArr.push(tmpObj)
			}
		})
		local.saveGoldData(tmpArr,room.players[room.getLocal().curPlayer].uid)
		//扣除服务费
		room.forEachReady(function(player) {
			room.getLocal().changeScore(player.chair,-room.charge)
		})
		//写分
		room.forEachReady(function(player) {
			var gold = room.getLocal().councilScores[player.chair]
			if(gold){
				player.gold += gold
				room.writeScore(player.uid,gold)
				if(player.uid > 10000){
					allGold += gold
					allGold += room.charge * room.rate
				}
			}
		})
		var notify = {
			cmd : "settlement",
			win : room.getLocal().curPlayer,
			playerHandler : room.getLogic().playerHandler,
			councils : room.getLocal().councilScores,
			bombs : room.boms
		}
		room.sendAll(notify)
		room.gameSettlement()
		room.forEachActive(function(player) {
			if(room.willWin && player.uid > 10000){
				player.playerInfo.goldPlayCount++
				// console.log("player.playerInfo.goldPlayCount",player.playerInfo.goldPlayCount)
				var notify = {
					cmd : "goldPlayCountIncrease"
				}
				room.sendByUid(player.uid,notify)
				if(player.playerInfo.goldPlayCount >= 20){
					var notify = {
						cmd : "userQuitCause",
						type : "goldPlayCountOver",
						gold : player.gold
					}
					room.sendByUid(player.uid,notify)
					room.userQuitBefore(player.uid)
					return
				}
			}
			if(player.gold > room.maxScore){
				var notify = {
					cmd : "userQuitCause",
					type : "goldMore",
					gold : player.gold
				}
				room.sendByUid(player.uid,notify)
				room.userQuitBefore(player.uid)
			}else if(player.gold < room.minScore){
				var notify = {
					cmd : "userQuitCause",
					type : "goldLow",
					gold : player.gold
				}
				room.sendByUid(player.uid,notify)
				room.userQuitBefore(player.uid)
			}else if(!player.isOnline){
				room.userQuitBefore(player.uid)
			}
		})
		//更新库存
		room.updateInventory(-allGold)
	}
	room.userQuitBefore = function(uid,cb) {
		// console.log("userQuitBefore1111111",uid,room.willWin)
		if(uid > 10000){
			if(room.willWin){
				var goldPlayCount = room.players[room.chairMap[uid]].playerInfo.goldPlayCount
				// console.log("userQuitBefore",goldPlayCount)
				room.GameRemote.app.rpc.db.remote.setValue(null,uid,"goldPlayCount",goldPlayCount,null)
			}
		}
		room.userQuit(uid,cb)
	}
	//更新金币数据
	local.saveGoldData = function(obj,win) {
		room.GameRemote.app.rpc.db.hunanRunQuickly.saveGoldData(null,obj,win,null)
	}
	return room
}
